package com.studerb.service;

import static com.studerb.util.Constants.EVENT_SERVICE_NAME;

import org.apache.log4j.Logger;
import org.bushe.swing.event.EventServiceLocator;
import org.bushe.swing.event.ThreadSafeEventService;

import com.studerb.service.interfaces.IService;

public class EventService extends ThreadSafeEventService implements IService {
    private boolean started = false;
    Logger logger = Logger.getLogger(EventService.class);

    //private ConsoleListener consoleListener;

    @Override public boolean isStarted() {
        return this.started;
    }

    @Override public void start() {
        if(started)
            throw new RuntimeException("EventService Already Started...");
        logger.info("[**EventService Starting**]");
        try{
            EventServiceLocator.setEventService(EVENT_SERVICE_NAME, this);
        }
        catch(Exception e){
            throw new RuntimeException("Error starting EventService: ", e);
        }
        /*consoleListener =  new ConsoleListener();
        this.subscribe(Object.class, consoleListener);
        StringEvent stringEvent = new StringEvent("From EventService Start()");
        logger.debug("Publishing test StringEvent");
        this.publish(stringEvent);
        logger.debug("Publishing Object Event");
        this.publish(new Object());
        */
        started = true;
    }

    @Override public void stop() {
        if( !started)
            throw new RuntimeException("EventService Already Stopped...");
        logger.info("[**EventService Stopping**]");
        started = false;
    }
}
